package com.customer.ostrich.controller;

import com.customer.ostrich.feignApi.CustomerClient;
import com.customer.ostrich.util.FeignUtil;
import feign.Feign;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import feign.okhttp.OkHttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author HuTongFu
 * @description:
 * @since 2019/6/21 13:38
 */
@Slf4j
@RestController
@RequestMapping("/customer")
public class TestController {

    @GetMapping("/testFeignApi")
    public String testFeignApi(String param) {
        CustomerClient target = Feign.builder()
                .client(new OkHttpClient())
                .encoder(new JacksonEncoder())
                .decoder(new JacksonDecoder())
                .target(CustomerClient.class, "http://localhost:8850/nacos-provider/");
        HelloWorld ostrich = target.testFeignProvider();
//        HelloWorld ostrich = target.testFeignProvider("OpenFeign");
        System.out.println(ostrich);

//        CustomerClient github = Feign.builder()
//                .decoder(new JacksonDecoder())
//                .target(CustomerClient.class, "https://api.github.com");
//        List<Contributor> contributors = github.contributors("OpenFeign", "feign");
//        for (Contributor contributor : contributors) {
//            System.out.println(contributor.getLogin() + " (" + contributor.getContributions() + ")");
//        }
        return "hello World ! ";
    }

    @PostMapping("/testFeignApiHasParameterC")
    public String testFeignApiHasParameter() {
        CustomerClient target = Feign.builder()
                .client(new OkHttpClient())
                .encoder(new JacksonEncoder())
                .decoder(new JacksonDecoder())
                .target(CustomerClient.class, "http://localhost:8850/nacos-provider/");
        HelloWorld ostrich = target.testFeignProviderHasParameter("OpenFeign");
        return "hello World ! 我是另一个服务返回的结果哦！！！" + ostrich;
    }

    @PostMapping("/testFeignApiHasParameterB")
    public String testFeignApiHasParameterBJson() {

        CustomerClient send = new FeignUtil<CustomerClient>().send(CustomerClient.class);
//        Map<String, Object> map = new HashMap<>();
//        map.put("test", "feign");
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.setParam("test");
//        String strJson = JSON.toJSONString(map);
//        Map<String, Object> parameters = new LinkedHashMap<>();
//        parameters.put("param", "test");
//        HelloWorld ostrich = target.testFeignApiHasParameterBJson(parameters);
        HelloWorld ostrich = send.testFeignApiHasParameterBJson(helloWorld);
        return "hello World ! 我是另一个服务返回的结果哦！！！" + ostrich;
    }
}
